home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 July / CD 3 / redhat-6.2.iso / RedHat / instimage / usr / lib / anaconda / iw / network.py < prev    next >
Encoding:
Python Source  |  2000-03-08  |  8.7 KB  |  245 lines

  1. from gtk import *
  2. from iw import *
  3. from isys import *
  4. from translate import _
  5.  
  6. class NetworkWindow (InstallWindow):        
  7.  
  8.     def __init__ (self, ics):
  9.     InstallWindow.__init__ (self, ics)
  10.  
  11.         ics.setTitle (_("Network Configuration"))
  12.         ics.setNextEnabled (1)
  13.         ics.readHTML ("netconf")
  14.         self.todo = ics.getToDo ()
  15.         self.calcNMHandler = None
  16.  
  17.         for dev in self.todo.network.available ().values ():
  18.             dev.set (("onboot", "yes"))  # TEMPRORY FIX UNTIL TODO SETS THIS
  19.  
  20.     def getNext (self):
  21.     if not self.__dict__.has_key("gw"):
  22.         return None
  23.         self.todo.network.gateway = self.gw.get_text ()
  24.         self.todo.network.primaryNS = self.ns.get_text ()
  25.         self.todo.network.secondaryNS = self.ns2.get_text ()
  26.         self.todo.network.ternaryNS = self.ns3.get_text ()
  27.         if (self.hostname.get_text () != ""):
  28.             self.todo.network.hostname = self.hostname.get_text ()
  29.         return None
  30.  
  31.     def focusInIP (self, widget, event, (ip, nm)):
  32.         if nm.get_text() == "":
  33.             self.calcNetmask (None, (ip, nm))
  34.             ip.calcNMHandler = ip.connect ("changed", self.calcNetmask, (ip, nm))
  35.  
  36.     def focusOutIP (self, widget, event, ip):
  37.         if (self.hostname.get_text () == ""
  38.             and self.todo.network.hostname != "localhost.localdomain"):
  39.             self.hostname.set_text (self.todo.network.hostname)
  40.  
  41.         if ip.calcNMHandler != None:
  42.             ip.disconnect (ip.calcNMHandler)
  43.             ip.calcNMHandler = None
  44.  
  45.     def focusOutNM (self, widget, event, (dev, ip, nm, nw, bc)):
  46.         try:
  47.             network, broadcast = inet_calcNetBroad (ip.get_text (), nm.get_text ())
  48.             if nw.get_text () == "":
  49.                 nw.set_text (network)
  50.                 dev.set (("network", network))
  51.             if bc.get_text () == "":
  52.                 bc.set_text (broadcast)
  53.                 dev.set (("broadcast", broadcast))
  54.         except:
  55.             pass
  56.  
  57.     def focusOutBC (self, widget, event, dev):
  58.         if self.gw.get_text () == "":
  59.             try:
  60.                 gw = inet_calcGateway (widget.get_text ())
  61.                 self.gw.set_text (gw)
  62.             except:
  63.                 pass
  64.  
  65.     def focusOutNW (self, widget, event, dev):
  66.         if self.ns.get_text () == "":
  67.             try:
  68.                 ns = inet_calcNS (widget.get_text ())
  69.                 self.ns.set_text (ns)
  70.             except:
  71.                 pass
  72.             
  73.     # not currently used
  74.     def setupTODO (self):
  75.         if self.devs:
  76.             if self.DHCPcb.get_active ():
  77.                 self.dev.set (("bootproto", "dhcp"))
  78.                 self.dev.unset ("ipaddr", "netmask", "network", "broadcast")
  79.             else:
  80.                 try:
  81.                     network, broadcast = inet_calcNetBroad (self.ip.get_text (), self.nm.get_text ())
  82.                     self.dev.set (("bootproto", "static"))
  83.                     self.dev.set (("ipaddr", self.ip.get_text ()), ("netmask", self.nm.get_text ()),
  84.                                   ("network", network), ("broadcast", broadcast), ("onboot", "yes"))
  85.                     self.todo.network.gateway = self.gw.get_text ()
  86.                     self.todo.network.primaryNS = self.dns1.get_text ()
  87.                 except:
  88.                     pass
  89.             
  90.                 self.dev.set (("onboot", "yes"))
  91.  
  92.  
  93.     def calcNWBC (self, widget, (dev, ip, nm, nw, bc)):
  94.         for addr in (ip, nm):
  95.             dots = 0
  96.             for ch in addr.get_text ():
  97.                 if ch == '.':
  98.                     dots = dots + 1
  99.             if dots != 3: return
  100.  
  101.         dev.set (("ipaddr", ip.get_text ()))
  102.         dev.set (("netmask", nm.get_text ()))
  103.  
  104.         
  105.     def calcNetmask (self, widget, (ip, nm)):
  106.         ip = ip.get_text ()
  107.         dots = 0
  108.         for x in ip:
  109.             if x == '.':
  110.                 dots = dots + 1
  111.         if dots != 3: return
  112.  
  113.         new_nm = inet_calcNetmask (ip)
  114.         if (new_nm != nm.get_text ()):
  115.             nm.set_text (new_nm)
  116.  
  117.     def DHCPtoggled (self, widget, (dev, table)):
  118.     active = widget.get_active ()
  119.         table.set_sensitive (not active)
  120.         self.ipTable.set_sensitive (not active)
  121.     
  122.     bootproto = "dhcp"
  123.     if not active:
  124.             bootproto = "static"
  125.     dev.set (("bootproto", bootproto))
  126.  
  127.     def onBootToggled (self, widget, dev):
  128.     if widget.get_active ():
  129.         onboot = "yes"
  130.     else:
  131.         onboot = "no"
  132.     dev.set (("onboot", onboot))
  133.  
  134.     def getScreen (self):
  135.         box = GtkVBox ()
  136.         box.set_border_width (5)
  137.         
  138.         notebook = GtkNotebook ()
  139.         devs = self.todo.network.available ()
  140.         if not devs: return None
  141.  
  142.         devs.keys ().sort ()
  143.         for i in devs.keys ():
  144.             devbox = GtkVBox ()
  145.             align = GtkAlignment ()
  146.             DHCPcb = GtkCheckButton (_("Configure using DHCP"))
  147.  
  148.             align.add (DHCPcb)
  149.             devbox.pack_start (align, FALSE)
  150.  
  151.             align = GtkAlignment ()
  152.             bootcb = GtkCheckButton (_("Activate on boot"))
  153.             bootcb.set_active (devs[i].get ("onboot") == "yes")
  154.         bootcb.connect ("toggled", self.onBootToggled, devs[i])
  155.             align.add (bootcb)
  156.  
  157.             devbox.pack_start (align, FALSE)
  158.  
  159.             devbox.pack_start (GtkHSeparator (), FALSE, padding=3)
  160.  
  161.             options = [(_("IP Address"), "ipaddr"),
  162.                        (_("Netmask"),    "netmask"),
  163.                        (_("Network"),    "network"),
  164.                        (_("Broadcast"),  "broadcast") ]
  165.             ipTable = GtkTable (len (options), 2)
  166.             self.ipTable = GtkTable (len (options), 2) # this is the iptable used for DNS, et. al
  167.  
  168.             DHCPcb.connect ("toggled", self.DHCPtoggled, (devs[i], ipTable))
  169.             DHCPcb.set_active (devs[i].get ("bootproto") == "dhcp" or
  170.                                devs[i].get ("bootproto") == "")
  171.  
  172.             forward = lambda widget, box=box: box.focus (DIR_TAB_FORWARD)
  173.  
  174.             for t in range (len (options)):
  175.                 label = GtkLabel ("%s:" % (options[t][0],))
  176.                 label.set_alignment (0.0, 0.5)
  177.                 ipTable.attach (label, 0, 1, t, t+1, FILL, 0, 10)
  178.                 entry = GtkEntry (15)
  179.                 # entry.set_usize (gdk_char_width (entry.get_style ().font, '0')*15, -1)
  180.                 entry.set_usize (7 * 15, -1)
  181.                 entry.connect ("activate", forward)
  182.                 entry.set_text (devs[i].get (options[t][1]))
  183.                 options[t] = entry
  184.                 ipTable.attach (entry, 1, 2, t, t+1, 0, FILL|EXPAND)
  185.  
  186.             for t in range (len (options)):
  187.                 if t == 0 or t == 1:
  188.                     options[t].connect ("changed", self.calcNWBC, (devs[i],) + tuple (options))
  189.  
  190.             self.focusOutNM (None, None, (devs[i],) + tuple (options))
  191.  
  192.             # add event handlers for the main IP widget to calcuate the netmask
  193.             options[0].connect ("focus_in_event", self.focusInIP, (options[0], options[1]))
  194.             options[0].connect ("focus_out_event", self.focusOutIP, options[0])
  195.             options[1].connect ("focus_out_event", self.focusOutNM, (devs[i],) + tuple (options))
  196.             options[2].connect ("focus_out_event", self.focusOutNW, devs[i])
  197.             options[3].connect ("focus_out_event", self.focusOutBC, devs[i])
  198.  
  199.             devbox.pack_start (ipTable, FALSE, FALSE, 5)
  200.  
  201.             devbox.show_all ()
  202.             notebook.append_page (devbox, GtkLabel (i))
  203.  
  204.         box.pack_start (notebook, FALSE)
  205.         box.pack_start (GtkHSeparator (), FALSE, padding=10)
  206.  
  207.         options = [_("Hostname"),
  208.                    _("Gateway"), _("Primary DNS"), _("Secondary DNS"), _("Ternary DNS")]
  209.  
  210.         for i in range (len (options)):
  211.             label = GtkLabel ("%s:" % (options[i],))
  212.             label.set_alignment (0.0, 0.0)
  213.             self.ipTable.attach (label, 0, 1, i, i+1, FILL, 0, 10)
  214.             if i == 0:
  215.                 options[i] = GtkEntry ()
  216.                 options[i].set_usize (7 * 30, -1)
  217.             else:
  218.                 options[i] = GtkEntry (15)
  219.                 options[i].set_usize (7 * 15, -1)
  220.             options[i].connect ("activate", forward)
  221.             align = GtkAlignment (0, 0.5)
  222.             align.add (options[i])
  223.             self.ipTable.attach (align, 1, 2, i, i+1, FILL, 0)
  224.         self.ipTable.set_row_spacing (0, 5)
  225.  
  226.         self.hostname = options[0]
  227.  
  228.         # bring over the value from the loader
  229.         if (self.todo.network.hostname != "localhost.localdomain"):
  230.             self.hostname.set_text (self.todo.network.hostname)
  231.  
  232.         self.gw = options[1]
  233.         self.gw.set_text (self.todo.network.gateway)
  234.  
  235.         self.ns = options[2]
  236.         self.ns.set_text (self.todo.network.primaryNS)
  237.  
  238.         self.ns2 = options[3]
  239.         self.ns2.set_text (self.todo.network.secondaryNS)
  240.  
  241.         self.ns3 = options[4]
  242.         self.ns3.set_text (self.todo.network.ternaryNS)
  243.         box.pack_start (self.ipTable, FALSE, FALSE, 5)
  244.         return box
  245.